﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using System.Windows.Media;
using System.Collections.Generic;
using System.Windows.Controls;

namespace PhilosophyQuiz
{
    public partial class MainWindow : Window
    {
        private Team team = new Team();
        public Question question = new Question();
        private DispatcherTimer timer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 1) };

        private int teamIndex = 0;
        private int time = 3;
        private bool gameStarted = false;
		private bool IsQuestionTaken = false;
		private bool IsTeamSelected = false;
        private List<Tuple<string, bool>> answer;
        private string correctAnswer;
		private int counter = 0;
        public MainWindow()
        {
            InitializeComponent();
            timer.Tick += new EventHandler(timer_Tick);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var windowCategories = new CategoryChooser();
            windowCategories.Show();
        }

        private void Grid_KeyDown(object sender, KeyEventArgs e)
        {
            if (team.List.Count != 0)
            {
                if (!timer.IsEnabled)
                {
                    if (e.Key == Key.Space)
                    {
                        textAnswer.Text = String.Empty;
                        //buttonAnswered.Visibility = System.Windows.Visibility.Visible;
                        textTime.Text = time.ToString();
                        ChooseRandomQuestion(Question.Subject.BEL);
						ChangeButtonsState(false);
						IsTeamSelected = false;
						IsQuestionTaken = true;
						textBlockTeam.Text = String.Empty;
                    }
                }
            }
            else
            {
                MessageBox.Show("Error 001: Трябва първо да добавите отбори.");
            }
			if (!IsTeamSelected && IsQuestionTaken)
			{
				switch (e.Key)
				{
					case Key.D1:
						IsTeamSelected = true;
						GetPlayingTeam(0);
						break;
					case Key.D2:
						IsTeamSelected = true;
						GetPlayingTeam(1);
						break;
					case Key.D3:
						IsTeamSelected = true;
						GetPlayingTeam(2);
						break;
					case Key.D4:
						IsTeamSelected = true;
						GetPlayingTeam(3);
						break;
				}
			}
			else
			{
				MessageBox.Show("Error 002: Трябва първо да изтеглите въпрос.");
			}
        }

        void timer_Tick(object sender, EventArgs e)
        {
            int time = int.Parse(textTime.Text);
            if (time > 0)
            {
                textTime.Text = (time - 1).ToString();
            }
            else
            {
                timer.Stop();
                textTime.Text = "Времето изтече!";
                ShowAnswer();
				IsTeamSelected = false;
            }
        }

        private void buttonAddTeam_Click(object sender, RoutedEventArgs e)
        {
            if (!gameStarted)
            {
                var window = new Registration();
                window.Show();
            }
            else
            {
                MessageBox.Show("Error 002: Не можете да добавяте отбори след като играта е започнала.");
            }
        }

        private void ChooseRandomQuestion(Question.Subject subject)
        {
            var randomQ = new Random();
            int choosen = randomQ.Next(question.QuestionsCount);
            textBlockQuestion.Text = question.GetXmlData(subject)[choosen].QuestionT;
            answer = question.GetXmlData(subject)[choosen].Answers;
            foreach (var answerTuple in question.GetXmlData(subject)[choosen].Answers)
            {
                if (answerTuple.Item2 == true)
                {
                    correctAnswer = answerTuple.Item1;
                }
            }
            buttonAnswer1.Content = answer[0].Item1;
            buttonAnswer2.Content = answer[1].Item1;
            buttonAnswer3.Content = answer[2].Item1;
            buttonAnswer4.Content = answer[3].Item1;
        }

        private void GetPlayingTeam(int index)
        {
			ChangeButtonsState(true);
			gameStarted = true;
			timer.Start();
			textBlockPoints.Text = team.GetPoints(index);
			textBlockTeam.Text = team.List[index].Name;
			var bc = new BrushConverter();
        }

        private void ShowAnswer()
        {
            textTime.Text = String.Format("Верният отговор е {0}!", correctAnswer);
        }

        private void buttonAnswer_Click(object sender, RoutedEventArgs e)
        {
            var button = (Button)sender;
            if (button.Content.ToString() == correctAnswer)
            {
                textBlockPoints.Text = (int.Parse(textBlockPoints.Text) + 1).ToString();
                team.IncreasePoints(teamIndex);
            }
            else
            {
                textTime.Text = "Грешен отговор!";
            }
            timer.Stop();
            ShowAnswer();
            ChangeButtonsState(false);
        }

        private void ChangeButtonsState(bool IsEnabled)
        {
            buttonAnswer1.IsEnabled = IsEnabled;
            buttonAnswer2.IsEnabled = IsEnabled;
            buttonAnswer3.IsEnabled = IsEnabled;
            buttonAnswer4.IsEnabled = IsEnabled;
        }

    }
}
